#ifndef QUERY_CONDITION_H
#define QUERY_CONDITION_H 1

#include <stdarg.h>

#include <common/macro/build.h>
#include <common/predicate.h>
#include <query/query.h>
#include <storage/tuple/tuple.h>
#include <table/table.h>
#include <transaction/types.h>
#include <transaction/transaction.h>


typedef enum condition_type_e {
	CONDITION_CALLBACK,
	CONDITION_PREDICATE,
} condition_type_e;

struct condition_t {
	transaction_t *transaction;
	condition_type_e type;

	int positive;
	union {
		struct {
			int column_id;
			char *name;
			int argc;
			char **arguments;
			size_t *argsizes;
		} predicate;
		struct {
			int column_id;
			void *arguments;
			predicate_t *predicate;
		} compiled;
	
		struct {
			query_callback_f funcptr;
			void *ctx;
		} callback;
	};
};

API_PUBLIC condition_t *condition_predicate_new_list(int, char *, int, size_t *, char **);
API_PUBLIC condition_t *condition_predicate_new(int, char *, int, ...);
API_PUBLIC condition_t *condition_callback_new(query_callback_f, void *);
API_PUBLIC condition_t *condition_negate(condition_t *);
API_LIBLOCAL int condition_bind(condition_t *, transaction_t *, table_t *);
API_LIBLOCAL int condition_check(condition_t *, query_t *, void **);
API_PUBLIC void condition_destroy(condition_t *);

#endif
